package com.scst.controller.admin;

import com.scst.constant.StatusConstant;
import com.scst.dto.DishDTO;
import com.scst.dto.DishPageQueryDTO;
import com.scst.entity.Dish;
import com.scst.mapper.DishMapper;
import com.scst.result.PageResult;
import com.scst.result.Result;
import com.scst.service.DishService;
import com.scst.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags="新增菜品")
@Slf4j
public class DishController {
    @Autowired
    DishService dishService;
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<DishDTO> save(DishDTO dishDTO) {
        log.info("开始新增菜品：{}",dishDTO);
        dishService.saveWithFlavors(dishDTO);
        //清理缓存数据
        return Result.success(dishDTO);
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}",dishPageQueryDTO );
        PageResult pageResult=dishService .pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除:{}",ids);
        dishService .deleteBatch(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜单")
    public Result<DishVO> check(@PathVariable Long id) {
        log.info("查询菜单：{}",id);
        DishVO  dishVo =dishService .getByIdWithFlavor(id);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        //构造Redis中的key
        //查询Redis中是否存在菜品数据





        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        List<DishVO> list = dishService.listWithFlavor(dish);
        return Result.success(list);
    }
}
